package com.hxf.dao.user.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.hxf.common.PublicUtil;
import com.hxf.dao.BaseDaoImpl;
import com.hxf.dao.user.UserDao;
import com.hxf.dto.AdminRoleDto;
import com.hxf.dto.RoleDto;
import com.hxf.dto.UserDto;
import com.hxf.exception.DaoLevelException;
/**
 * DAO会员实现类
 * @author tengyicheng
 * @version [1.0, 2013/09/22]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@SuppressWarnings("unchecked")
@Repository("userDao")
public class UserDaoImpl extends BaseDaoImpl implements UserDao {
	
	public List<UserDto> findByUser(UserDto userDto, int targetPage, int pageSize)throws DaoLevelException { 	
 		if(null != userDto){
			String sql = "";
			//用户名称
			if (PublicUtil.isNotNullAndEmpty(userDto.getUserName())) {
				sql = " and u.user_name like '"+userDto.getUserName().trim()+"%'  ";
			}
			
			//用户类型
			if(PublicUtil.isNotNull(userDto.getUserType())){
				sql = " and u.user_type = " + userDto.getUserType();
			}
			
			//身份证号码
			if (PublicUtil.isNotNullAndEmpty(userDto.getUserIdNo())) {
				sql += " and u.user_id_no like '"+userDto.getUserIdNo().trim()+"%'  ";
			}
			
			//电话号码
			if (PublicUtil.isNotNullAndEmpty(userDto.getUserPhone())) {
				sql += " and u.user_phone like '"+userDto.getUserPhone().trim()+"%'  ";
			}
			
			//return this.findBy("from UserDto where 1=1"+sql, targetPage, pageSize);
			sql = " select * from (select user_id,create_time,ip,login_status,login_time,login_version,real_name,STATUS,user_address,user_email,user_id_no,user_mobile,user_name,user_phone,user_pwd, " +
				"user_relation,user_sex,user_type,user_zip_code,restaurant_id,parent_id,parent_user_id,qq,rank,credit from rk_user where 1=1 and parent_id in (select user_id from rk_user where user_id="
				+userDto.getUserId()+") or user_id = "+userDto.getUserId()+" ) as u where 1=1" + sql;
			
			List list = super.exeSQLList(sql);
			List<UserDto> userDtoList =  new ArrayList<UserDto>();
			UserDto dto = null;
			if(null != list || 0 != list.size()){
				for(int i=0;i<list.size();i++){
					Object[] obj = (Object[])list.get(i);
					dto = new UserDto();
					dto.setUserId(null==obj[0]?null:Integer.valueOf(obj[0].toString()));//user_id
					dto.setCreateTime(null==obj[1]?null:obj[1].toString());//创建时间
					dto.setLoginIp(null==obj[2]?null:obj[2].toString());//登录IP
					dto.setLoginStatus(null==obj[3]?null:Integer.valueOf(obj[3].toString()));
					dto.setLoginTime(null==obj[4]?null:obj[4].toString());//登录时间
					dto.setLoginVersion(null==obj[5]?null:obj[5].toString());
					dto.setRealName(null==obj[6]?null:obj[6].toString());//真实名称
					dto.setStatus(null==obj[7]?null:Integer.valueOf(obj[7].toString()));//状态
					dto.setUserAddress(null==obj[8]?null:obj[8].toString());//地址
					dto.setUserEmail(null==obj[9]?null:obj[9].toString());//Email
					dto.setUserIdNo(null==obj[10]?null:obj[10].toString());//身份证号码
					dto.setUserMobile(null==obj[11]?null:obj[11].toString());//固定电话
					dto.setUserName(null==obj[12]?null:obj[12].toString());//用户名
					dto.setUserPhone(null==obj[13]?null:obj[13].toString());//手机号码
					dto.setUserPwd(null==obj[14]?null:obj[14].toString());//密码
					dto.setUserRelation(null==obj[15]?null:obj[15].toString());
					dto.setUserSex(null==obj[16]?null:Integer.valueOf(obj[16].toString()));//性别
					dto.setUserType(null==obj[17]?null:Integer.valueOf(obj[17].toString()));//用户类型
					dto.setUserZipCode(null==obj[18]?null:obj[18].toString());
					dto.setRestaurantId(null==obj[19]?null:Integer.valueOf(obj[19].toString()));
					dto.setRestaurantUserId(null==obj[20]?null:Integer.valueOf(obj[20].toString()));
					//dto.setParentId(null==obj[21]?null:Integer.valueOf(obj[21].toString()));
					//dto.setParentUserId(null==obj[22]?null:obj[22].toString());
					userDtoList.add(dto);
				}
			}
			
			return userDtoList;
 		}
 		
 		return null;
	} 

	@Override
	public List<UserDto> findByUser(UserDto userDto) throws DaoLevelException {
		String sql = "";
		if (PublicUtil.isNotNullAndEmpty(userDto.getUserName())) {
			sql = " and userName like '"+userDto.getUserName()+"%'";
		}
		
		if (PublicUtil.isNotNullAndEmpty(userDto.getUserIdNo())) {
			sql += " and userIdNo like '"+userDto.getUserIdNo()+"%'";
		}
		
		if (PublicUtil.isNotNullAndEmpty(userDto.getUserPhone())) {
			sql += " and userPhone like '"+userDto.getUserPhone()+"%'";
		}
		return this.findBy("from UserDto where 1=1"+sql);
	}
	
	@Override
	public List<RoleDto> findByUserRole(AdminRoleDto urDto)throws DaoLevelException {
		String sql = "select distinct rr.role_id ,rr.role_name ,rr.remark,rr.status from rk_user as ru, rk_admin_role as rur,rk_role as rr where   ru.user_id = rur.user_id "+ 
					 "and rur.role_id = rr.role_id  and ru.user_id='"+urDto.getUserId()+"'"; 
		List list = this.exeSQLList(sql); 
		List<RoleDto> userRoleVoList = null;
		RoleDto urVo = null;
		if (list != null && list.size() > 0) {
			userRoleVoList = new ArrayList<RoleDto>();
			for (int i = 0;i < list.size(); i++) {
				Object [] o = (Object[]) list.get(i);
				urVo = new RoleDto();
				urVo.setRoleId(null==o[0]?null:Integer.parseInt(o[0].toString()));
				urVo.setRoleName(null==o[1]?null:o[1].toString());
				urVo.setRemark(null==o[2]?null:o[2].toString());
				urVo.setStatus(null==o[3]?null:Integer.parseInt(o[3].toString())); 
				userRoleVoList.add(urVo);
			}
		}
		return userRoleVoList;
	}

	@Override
	public int deleteUserRoleByUserId(Integer userId) throws DaoLevelException {
		return this.executeUpdateBySql("delete from rk_admin_role where user_id="+userId);
	}

	@Override
	public int loginUser(UserDto userDto) throws DaoLevelException { 
		List<UserDto> userList = this.findBy("from UserDto where userName = '"+userDto.getUserName()+"'");
		//用户名不存在
		if (userList == null || userList.size() == 0) {
			return -1;
		} 
		
		userList = this.findBy("from UserDto where userPwd = '"+userDto.getUserPwd()+"' and userName = '"+userDto.getUserName()+"'");
		//密码不存在
		if (userList == null || userList.size() == 0) {
			return -2;
		}  
		//账号审核中
		System.out.println("status:"+userList.get(0).getStatus());
		System.out.println("userId:"+userList.get(0).getUserId());
		if (userList.get(0).getStatus() == 1) {
			return -3;
		}
		//审核不通过
		if (userList.get(0).getStatus() == 2) {
			return -4;
		} 
		//锁定
		if (userList.get(0).getStatus() == 3) {
			return -5;
		}
		//作废
		if (userList.get(0).getStatus() == 4) {
			return -6;
		} 
		userDto.setUserId(userList.get(0).getUserId()); 
		
		return 0;
	}

	@Override
	public boolean validateUserName(String userName) throws DaoLevelException {
		List<UserDto> userList = this.findBy("from UserDto where userName = '"+userName+"'");
		//用户名不存在
		if (userList == null || userList.size() == 0) {
			return true;
		} 
		return false;
	}
	
	/*
	 * 获取登陆用户的角色
	 * @see com.hxf.dao.user.UserDao#selectUserRole(java.lang.Integer)
	 */
	@Override
	public AdminRoleDto selectUserRole(Integer userId) throws DaoLevelException{
		String sql = "SELECT role_id,r.user_id FROM rk_user AS u LEFT JOIN rk_admin_role r ON u.user_id=r.user_id WHERE u.user_id="+userId;
		List<?> roleDtoList = this.exeSQLList(sql);
		AdminRoleDto roleDto = new AdminRoleDto();
		if(null != roleDtoList){
			Object[] o = (Object[])roleDtoList.get(0);
			roleDto.setRoleId(o[0]==null?null:Integer.valueOf(o[0].toString()));
			roleDto.setUserId(o[1]==null?null:Integer.valueOf(o[1].toString()));
		}
		//System.out.println("::"+roleDto.toString());
		
		return roleDto;
	}
	
	//查询是否为 超级管理员
	public UserDto selectUserOrAdmin(Integer userId) throws DaoLevelException{
		List<UserDto> userOrAdmin = this.findBy(" from UserDto where userId="+userId);
		UserDto dto = null;
		if(userOrAdmin!=null && userOrAdmin.size() > 0){
			dto = userOrAdmin.get(0);
			return dto;
		}
		return null;
	}
}
